﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using PS_WSSDeploy_Package.Application;
using PS_WSSDeploy_Package.WdCommand;

namespace PS_WSSDeploy_Package.WdCommand
{
    class DeployFullCmd : BaseCmd
    {
        public static BaseCmd Create(OleMenuCommandService service, bool enableForSandbox)
        {
            var instance = new DeployFullCmd();
            var cmdId = new CommandID(GuidList.guidPS_WSSDeploy_PackageCmdSet, (int)PkgCmdIDList.cmdidDeployCommand);
            AddCommandToMenu(service, instance, cmdId, enableForSandbox);
            return instance;
        }

        public override void DoExecute()
        {
            //if (!File.Exists(WSPFullFileName))
            //{
                AddTask(new BuildCmd());
            //}
            if (!IsSharePointSandboxProject)
            {
                AddTask(new RecycleAppPoolsCmd());
            }
            AddTask(new DeployCmd());
        }

        public void TaskExecute()
        {
            try
            {
                //Cc.Create(WSPFullFileName);
                //WriteOutput(string.Format(WssDeployResources.CreateCmd_AddSolutionSucceeded, WSPFileName, WSPFileName));
                Cc.Deploy(WSPFullFileName, ProjectTargetFolder);
                WriteOutput(string.Format(WssDeployResources.DeployFullCmd_DeploySolutionSucceeded, WSPFileName));
            }
            catch (Exception ex)
            {
                WriteOutput(string.Format(WssDeployResources.DeployFullCmd_DeploySolutionError, WSPFileName, ex.Message));
            }
        }
    }
}
